package com.shadow.bus.main.alarm;

import com.alibaba.fastjson.TypeReference;
import com.shadow.bus.entity.AlarmTypeEntity;
import com.tepth.shadow.ui.recycler.ObjConverter;

import java.util.ArrayList;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

/**
 * @author zhangyu
 * @date 2018/7/11
 */

public class AlarmDataConverter {

    public void converter(final String response, final OnDataConverterListener listener) {
        Observable.create(new ObservableOnSubscribe<Object>() {
            @Override
            public void subscribe(ObservableEmitter<Object> e) throws Exception {
                ArrayList<AlarmTypeEntity> typeEntities = new ObjConverter<ArrayList<AlarmTypeEntity>>().
                        convert(response, new TypeReference<ArrayList<AlarmTypeEntity>>() {
                        });
                e.onNext(typeEntities);
            }
        })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.io())
                .subscribe(new Observer<Object>() {
                    @Override
                    public void onSubscribe(Disposable d) {

                    }

                    @SuppressWarnings("unchecked")
                    @Override
                    public void onNext(Object o) {
                        if(listener != null){
                            ArrayList<AlarmTypeEntity> list = (ArrayList<AlarmTypeEntity>)o;
                            listener.onConverter(list);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public interface OnDataConverterListener {
        void onConverter( ArrayList<AlarmTypeEntity> list);
    }
}
